package com.som.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class IpAddressUtil
{
	/**
	 * 将开始Ip与结束ip转化为集合
	 * 
	 * @param startIpinput
	 *            开始IP
	 * @param endIpinput
	 *            结束IP
	 * @return ip段内所有ip信息
	 */
	public static List<String> getIpListBySecation(String startIpinput, String endIpinput)
	{
		if (StringUtils.isNotBlank(startIpinput) && StringUtils.isNotBlank(endIpinput))
		{
			List<String> list = new ArrayList<String>();

			String startIp = startIpinput.trim();
			String endIp = endIpinput.trim();

			int startIpindex = startIp.lastIndexOf(".") + 1;

			// 取得ip开始循环值
			String ipStartFlag = startIp.substring(0, startIpindex);

			int ipStartNum = Integer.parseInt(startIp.substring(startIpindex));

			int endIpindex = endIp.lastIndexOf(".");

			// 取得结束的ip尾字符
			int ipEndOverNum = Integer.parseInt(endIp.substring(endIpindex + 1));

			// 如果结束的数字，大于开始的ip数字，则进行循环
			if (ipEndOverNum > ipStartNum)
			{
				// 循环得出ip段信息
				for (int i = ipStartNum; i <= ipEndOverNum; i++)
				{
					list.add(ipStartFlag + i);
				}
			}
			// 否则只放首地址
			else
			{
				list.add(startIpinput);
			}

			return list;
		}

		return null;
	}

	public static void main(String[] args)
	{
		List<String> list = getIpListBySecation("192.168.1.10", "192.168.1.15");

		System.out.println(list);
	}
}
